package com.kasax.base

import android.app.Activity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.kasax.lifecycle.BasicFragmentLifecycleObserver

abstract class BaseFragment : Fragment() {
    override fun onAttach(activity: Activity) {
        super.onAttach(activity)
        lifecycle.addObserver(BasicFragmentLifecycleObserver(this))
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        if (getLayoutId() == 0) {
            throw RuntimeException("getLayoutId() must be implemented")
        }
        return inflater.inflate(getLayoutId(), container, false)
    }

    abstract fun getLayoutId(): Int

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
        addListeners(view)
        initData()
    }

    open fun initData() {
    }

    open fun addListeners(view: View) {
    }

    open fun initView(view: View) {
    }
}